//
//  BaseViewController.m
//  Feast
//
//  Created by 胡光健 on 2017/8/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "BaseViewController.h"
#import "CCDatePickerView.h"
#import "HomeKeepModel.h"
@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HJColorRGB(255);
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
    [self createFMDB];
    [self createHomeFMDB];
}
- (void)setNavTitle:(NSString *)navTitle {
    _navTitle = navTitle;
    self.navigationItem.title = navTitle;
}

- (void)setLeftNavTitle:(NSString *)leftNavTitle {
    _leftNavTitle = leftNavTitle;
    if (self.leftNavImageName.length) {
        [self configLeftButton];
        return;
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:leftNavTitle style:UIBarButtonItemStylePlain target:self action:@selector(didClickLeftNavAction)];
}

- (void)setRightNavTitle:(NSString *)rightNavTitle {
    _rightNavTitle = rightNavTitle;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:rightNavTitle style:UIBarButtonItemStylePlain target:self action:@selector(didClickRightNavAction)];
}

- (void)setRightNavImageName:(NSString *)rightNavImageName {
    _rightNavImageName = rightNavImageName;
    UIImage *image = [[UIImage imageNamed:rightNavImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(didClickRightNavAction)];
}

- (void)setLeftNavImageName:(NSString *)leftNavImageName {
    _leftNavImageName = leftNavImageName;
    if (self.leftNavTitle.length) {
        [self configLeftButton];
        return;
    }
    UIImage *image = [UIImage imageNamed:leftNavImageName];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(didClickLeftNavAction)];
}
- (void)configLeftButton {
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 60, 30);
    leftBtn.titleLabel.font = HJFontSize(16);
    leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    leftBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [leftBtn setTitle:self.leftNavTitle forState:0];
    [leftBtn setTitle:self.leftNavTitle forState:UIControlStateHighlighted];
    [leftBtn setImage:[UIImage imageNamed:self.leftNavImageName] forState:0];
    [leftBtn setImage:[UIImage imageNamed:self.leftNavImageName] forState:UIControlStateHighlighted];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    [leftBtn addTarget:self action:@selector(didClickLeftNavAction) forControlEvents:UIControlEventTouchUpInside];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)didClickLeftNavAction {
    
}

- (void)didClickRightNavAction {
    
}
- (void)dealloc {
    [mNotificationCenter removeObserver:self];
}



-(void)configSegmentWithleftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle {
    UISegmentedControl * segment =[[UISegmentedControl alloc]init];
    segment.frame = CGRectMake(0, 0, 150, 30);
    [segment insertSegmentWithTitle:leftTitle atIndex:0 animated:YES];
    [segment insertSegmentWithTitle:rightTitle atIndex:1 animated:YES];
    segment.selectedSegmentIndex = 0;
    [segment addTarget:self action:@selector(segmentCilik:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segment;
}
-(void)segmentCilik:(id)sender {
}

#pragma mark --创建数据库
-(void)createFMDB {
    NSString * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString * dbPath = [path stringByAppendingPathComponent:@"sqlite.db"];
    FMDatabase * db = [FMDatabase databaseWithPath:dbPath];
    if ([db open]) {
        NSString * sql = @"CREATE TABLE IF NOT EXISTS t_Keep (content text, address text, image blob, time text, money text, tag long);";
        BOOL bl =[db executeUpdate:sql];
        if (bl) {
            NSLog(@"打开数据库成功");
        }else {
            NSLog(@"打开数据库失败");
        }
    }
    self.db = db;
    [db close];
}

-(void)createHomeFMDB {
    NSString * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString * dbPath = [path stringByAppendingPathComponent:@"homeSqlite.db"];
    NSLog(@"%@",dbPath);
    FMDatabase * homeDB = [FMDatabase databaseWithPath:dbPath];
    if ([homeDB open]) {
        NSString * sql = @"CREATE TABLE IF NOT EXISTS homeSpeeding (content text, image blob, time text, money text, tag long);";
        BOOL bl =[homeDB executeUpdate:sql];
        if (bl) {
            NSLog(@"打开首页数据库成功");
        }else {
            NSLog(@"打开首页数据库失败");
        }
    }
    self.homeDB = homeDB;
    [homeDB close];
}

#pragma mark -- 查询数据库
-(void)findFMDBWithData:(void (^)(NSMutableArray *dataArray))data {
    if ([self.db open]) {
        NSMutableArray * array = [NSMutableArray array];
        FMResultSet *resultSet = [self.db executeQuery:@"SELECT * FROM t_Keep"];
        while ([resultSet  next]) {
            HomeKeepModel * model = [HomeKeepModel new];
            model.content = [resultSet objectForColumn:@"content"];
            model.address = [resultSet objectForColumn:@"address"];
            model.image = [resultSet objectForColumn:@"image"];
            model.time = [resultSet objectForColumn:@"time"];;
            [array addObject:model];
            if (data) {
                data(array);
            }
        }
    }
    [self.db close];
}
-(void)findHomeFMDBWithData:(void (^)(NSMutableArray *dataArray ,NSNumber * speedingSum))data keep:(void(^)(NSMutableArray *keepArray, NSNumber * keepSum))keep {
    if ([self.homeDB open]) {
//        content text, image blob, time text, money text, tag integer
        NSMutableArray * array = [NSMutableArray array];
        NSMutableArray * keeps = [NSMutableArray array];
        NSMutableArray * allMoney =[NSMutableArray array];
        NSMutableArray * keepAllMoney =[NSMutableArray array];
        FMResultSet *resultSet = [self.homeDB executeQueryWithFormat:@"SELECT * FROM homeSpeeding"];
        while ([resultSet  next]) {
            HomeKeepModel * model = [HomeKeepModel new];
            model.content = [resultSet objectForColumn:@"content"];
            model.money = [resultSet objectForColumn:@"money"];
            model.image = [resultSet objectForColumn:@"image"];
            model.time = [resultSet objectForColumn:@"time"];
            model.ID = [resultSet intForColumn:@"tag"];
            if (model.ID == 1) {
                [allMoney addObject:[resultSet objectForColumn:@"money"]];
                [array addObject:model];
                if (data) {
                    data(array,[allMoney valueForKeyPath:@"@sum.floatValue"]);
                }
            }else {
                [keepAllMoney addObject:[resultSet objectForColumn:@"money"]];
                [keeps addObject:model];
                if (keep) {
                    keep(keeps,[keepAllMoney valueForKeyPath:@"@sum.floatValue"]);
                }
            }
        }
//        _speedingSum = [allMoney valueForKeyPath:@"@sum.floatValue"];
//        _keepAvg = [keepAllMoney valueForKeyPath:@"@sum.floatValue"];
        
        NSLog(@"%@",_speedingSum);
        NSLog(@"%@",_keepAvg);
    }
    [self.homeDB close];
}
-(void)setSpeedingSum:(NSNumber *)speedingSum {
    _speedingSum = speedingSum;
}
-(void)setKeepAvg:(NSNumber *)keepAvg {
    _keepAvg = keepAvg;
}
- (void)delete {
    [self.db executeUpdate:@"DROP TABLE IF EXISTS t_Keep;"];
    [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_Keep (id integer PRIMARY KEY AUTOINCREMENT, content text NOT NULL, address text NOT NULL, image text NOT NULL,time text NOT NULL);"];
    
    [self.db close];
}
@end
